package model.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.dto.Country;
import utility.Database;
import utility.Log;

public class CountryModel extends Database {
	
	public boolean insert(Country dto) {
		String sql = "insert into tbl_country(country_id,country_name,country_image) values(null,?,?)";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, dto.getName());
			pstmt.setString(2, dto.getImage());
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}

		return false;
	}

	public boolean delete(int country_id) {
		String sql = "delete from tbl_country where country_id=?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, country_id);
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return false;
	}

	public boolean update(Country dto) {
		String sql = "update tbl_country set country_name=?, country_image=? where country_id=?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, dto.getName());
			pstmt.setString(2, dto.getImage());
			pstmt.setInt(3, dto.getId());
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return false;
	}

	public Country get(int country_id) {
		String sql = "select * from tbl_country where country_id=?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, country_id);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()) {
				Country dto = new Country();
				dto.setId(country_id);
				dto.setName(rs.getString("country_name"));
				dto.setImage(rs.getString("country_image"));
				return dto;
			}
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return null;
	}
	
	public ResultSet list(String search, int limit, int page) {
		String sql = "select * from tbl_country where country_name like ? ORDER BY country_id DESC limit ?,?";
		try {
			//this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, "%" + search + "%");
			pstmt.setInt(2, (page - 1) * limit);
			pstmt.setInt(3, limit);
			ResultSet rs = pstmt.executeQuery();
			return rs;
		} catch (SQLException e) {
			Log.write(e);
		} 
		return null;
	}
	public ResultSet list() {
		String sql = "select * from tbl_country ORDER BY country_id";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			return rs;
		} catch (SQLException e) {
			Log.write(e);
		} 
		return null;
	}
	
	public ResultSet list(int limit, int page) {
		return list("", limit, page);
	}
	
	public int count(String search) {
		String sql = "select count(*) from tbl_country where country_name like ?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, "%" + search + "%");
			ResultSet rs = pstmt.executeQuery();
			return rs.next() ? rs.getInt(1) : 0;
		} catch (SQLException e) {
			Log.write(e);
		}
		return 0;
	}

	public int count() {
		return count("");
	}
	
	public int getLastIndex(){
		String sql="select max(country_id) from tbl_country";
		try {
			this.connect();
			PreparedStatement pstmt=con.prepareStatement(sql);
			ResultSet rs=pstmt.executeQuery();
			if(rs.next()) 
				return rs.getInt(1);
		} catch (SQLException e) {
			Log.write(e);
		}
		return 0;
	}
}
